package com.controller;

//import com.domain.User;
//import com.entity.FUser;
import com.entity.QUser;
//import com.entity.table.FUserTableDef;
import com.entity.table.QUserTableDef;
//import com.mapper.FUserMapper;
//import com.mapper.UserMapper;
import com.mybatisflex.core.query.QueryWrapper;
//import com.service.impl.FUserServiceImpl;
import com.service.impl.QUserServiceImpl;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.management.Query;

@Controller
public class LoginController {
//    @Autowired
//    FUserMapper userMapper;
    @Autowired
    QUserServiceImpl qUserService;
    @GetMapping("/login")
    public String toLoginPage(){
        return "client/login";
    }
    //登录
    @PostMapping("/toLogin")
    //ResponseBody
    public String login(QUser user, HttpSession httpSession, Model model){
//        FUser tUser = userMapper.userLogin(user);
        QueryWrapper query = QueryWrapper.create()
                .where(QUserTableDef.Q_USER.USERNAME.eq(user.getUsername()))
                .and(QUserTableDef.Q_USER.PASSWORD.eq(user.getPassword()));
        QUser tUser = qUserService.getOne(query);
        if(tUser!=null){
            //将用户对象添加到Session
            httpSession.setAttribute("user",tUser);
            model.addAttribute("user",tUser);
            //跳转到主页面
            return "redirect:/";
            //跳转到后台主页
//            return "client/index";

        }else {
            model.addAttribute("msg","账号或者密码错误,请重新输入!");
            return "client/login";
        }
    }
}
